<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            title="朋友圈精准投放"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                * 请选择需要精准投送朋友圈的微信号
                <el-form-item prop="select_type" label="录入方式" required>
                    <el-radio-group v-model="formData.select_type">
                        <el-radio :value="1">手动录入</el-radio>
                        <el-radio :value="2">全量投放</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item prop="wechat_tags" label="投放账号"  v-if="formData.select_type == 1">
                    <el-tag size='large' v-for="tag in dynamicTags" :key="tag" class="mx-1" closable :disable-transitions="false" @close="handleClose(tag)">
                        {{tag}}
                    </el-tag>

                    <el-input v-if="wechatInputVisible" ref="InputRef" class="ml-1 w-20"  v-model="wechatInputValue"
                        size="default" @keyup.enter="handleWechatInputConfirm" @blur="handleWechatInputConfirm">
                    </el-input>
                    <el-button v-else class="button-new-tag ml-1" size="default" @click="showWechatInput"> + 新增</el-button>
                </el-form-item>

            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup  name="tencentAdvertisingUserAdListEdit" >
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {apiTencentAdvertisingUserAdListPlacement, apiTencentAdvertisingUserAdListDetail } from '@/api/tencent_advertising_user_ad_list'
import { ElLoading,ElInput } from 'element-plus'
import type { PropType } from 'vue'
import feedback from '@/utils/feedback'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success','close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 表单数据
const formData = reactive({
    id:0,
    select_type: 1,
    tag_wechats:[]
})

const tf_data=reactive({
    val:'',
    cities:[],
    total:0,
    pagesize:1,
    limit:10,
})


const dynamicTags = ref([])

const wechatInputValue = ref('')
const wechatInputVisible = ref(false)
const InputRef = ref<InstanceType<typeof ElInput>>()


const handleClose  = (tag: string) => {
    dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1)
}

const showWechatInput = () => {
    wechatInputVisible.value = true
  nextTick(() => {
    InputRef.value!.input!.focus()
  })
}


const handleWechatInputConfirm = () => {
  if (wechatInputValue.value) {
    dynamicTags.value.push(wechatInputValue.value)
  }
  wechatInputVisible.value = false
  wechatInputValue.value = ''
}

// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    // formData.value.id=data.id
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    console.log(formData,'44444444444')
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiTencentAdvertisingUserAdListDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    const loading = ElLoading.service({ lock: true, text: 'Loading...', background: 'rgba(0, 0, 0, 0.7)', })
    await formRef.value?.validate()

    const data = { ...formData,...{wechat_list:dynamicTags.value} }

    // mode.value == await apiTencentAdvertisingUserAdListPlacement(data)
    let res = await apiTencentAdvertisingUserAdListPlacement(data)
    console.log(res,'resresresresresresresresresresresresres')
    loading.close()
    if(res.data.code == 1){
        feedback.msgSuccess(res.data.msg)
    }else{
        feedback.msgError(res.data.msg)
    }
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
